package com.iot.core.exception;

import org.springframework.util.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

public class MyExceptionUtil {
    public  static  String getMsg(Exception ex){
        if(ex==null){
            return "";
        }
        String msg=ex.getMessage();
        if(msg!=null&&msg!=""){
            return msg;
        }
        return ex.toString();
    }
    public  static  String getFullmsg(Exception ex){
        if(ex==null){
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        // 将出错的栈信息输出到printWriter中
        ex.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }
    public  static  String getExClsname(Exception ex) {
        if(ex==null){
            return "";
        }
        var r="";
        try {
            r = ex.getClass().getName();
            if(!StringUtils.hasText(r)){
                r="";
            }else if(r.length()>50){
                r=r.substring(0,50);
            }
        }
        catch(Exception ex2){
        }
        return r;
    }
    public  static  String getExMsg(Exception ex) {
        if(ex==null){
            return "";
        }
        var r="";
        try {
            r = ex.getMessage();
            if(!StringUtils.hasText(r)){
               return  getExClsname(ex);
            }else if(r.length()>200){
                r=r.substring(0,200);
            }
        }
        catch(Exception ex2){
        }
        return r;
    }

}
